package com.ptone.messageCenter.config;

import com.ptone.messageCenter.api.ConsumerAPI;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ContainerProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * kafka消费者配置
 *
 * @author Lvjiapeng
 */
@Configuration
@EnableKafka
public class KafkaConsumerConfig {

    @Value("${consumer.bootstrap.servers}")
    private String servers;
    @Value("${consumer.group.id}")
    private String groupId;
    @Value("${consumer.enable.auto.commit}")
    private Boolean enableAutoCommit;
    @Value("${consumer.auto.commit.interval.ms}")
    private Integer autoCommitInterval;
    @Value("${consumer.auto.offset.reset}")
    private String autoOffsetReset;
    @Value("${consumer.session.timeout.ms}")
    private Integer sessionTimeout;
    @Value("${consumer.key.deserializer}")
    private String keyDeserializer;
    @Value("${consumer.value.deserializer}")
    private String valueDeserializer;
    @Value("${consumer.poll.timeout}")
    private Long pollTimeout;
    @Value("${consumer.max.poll.records}")
    private Integer maxPollRecords;

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.getContainerProperties().setPollTimeout(pollTimeout);
        return factory;
    }

    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }


    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializer);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer);
        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        propsMap.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords);
        return propsMap;
    }


    @Bean("ackContainerFactory")
    public ConcurrentKafkaListenerContainerFactory ackContainerFactory() {
        ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory();
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
        factory.setConsumerFactory(new DefaultKafkaConsumerFactory(consumerConfigs()));
        factory.setAutoStartup(true);
        //防止topic不存在的时候报错
        factory.setMissingTopicsFatal(false);
        return factory;
    }


    /**
     * kafka监听
     *
     * @return
     */
    @Bean
    public ConsumerAPI listener() {
        return new ConsumerAPI();
    }
}